package top.infopub.mgr.sys.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sys.dao.OperatorroleDao;
import top.infopub.mgr.sys.dao.RoleDao;
import top.infopub.mgr.sys.dao.RolepurviewDao;
import top.infopub.mgr.sys.service.PurviewService;
import top.infopub.mgr.sys.service.RoleService;
import top.infopub.model.sys.Role;
import top.infopub.model.sys.Rolepurview;
import top.infopub.security.service.SysRoleSevice;
import top.infopub.security.vo.RoleVO;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;


@Service
public class RoleServiceImpl extends GenericServiceImpl<Role, String> implements RoleService, SysRoleSevice {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RolepurviewDao rolepurviewDao;

    @Autowired
    private PurviewService purviewService;

    @Autowired
    private OperatorroleDao operatorroleDao;

    @Override
    public GenericDao<Role, String> getDao() {
        return roleDao;
    }

    @Override
    public List<RoleVO> queryAllRole() {
        return null;
    }

    @Override
    public List<RoleVO> queryRoleByUserId(String userId) {
        List<RoleVO> voArr = new ArrayList<RoleVO>();
        List<Role> entity = this.roleDao.selectByUserId(userId);
        if (entity != null) {
            for (Role r : entity) {
                RoleVO vo = new RoleVO();
                vo.setRoleId(r.getId());
                vo.setRoleCode(r.getSroleCode());
                vo.setRoleName(r.getSroleName());
                voArr.add(vo);
            }
        }
        return voArr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] checkboxId) {
        for (String id : checkboxId) {
            operatorroleDao.deleteByRolerId(id);
            rolepurviewDao.deleteByRoleId(id);
            roleDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRolePurview(String checkPurviewId, String roleId) {
        // 删除已经存在的权限
        rolepurviewDao.deleteByRoleId(roleId);
        if (StringUtils.isNotBlank(checkPurviewId)) {
            List<Rolepurview> rolepurviews = new ArrayList<Rolepurview>();
            String[] purIds = checkPurviewId.split(",");
            if (purIds != null && purIds.length > 0) {
                for (String purId : purIds) {
                    //Purview purview = purviewService.selectByPrimaryKey(purId);
                    //if(purview != null && purview.getId() != null) {
                    Rolepurview rolepurview = new Rolepurview();
                    String id = IdFactory.getUUIDUpper();
                    rolepurview.setId(id);
                    rolepurview.setSpurviewId(purId);
                    rolepurview.setSroleId(roleId);
                    rolepurviews.add(rolepurview);
                    //rolepurviewDao.insert(rolepurview);
                    //}
                }
                //批量插入权限记录
                if (CollectionUtils.isNotEmpty(rolepurviews)) {
                    rolepurviewDao.insertByBatch(rolepurviews);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRolePurviewByClickMenu(String checkPurviewId, String roleId, String clickMenuId) {
        Set<String> ids = Sets.newHashSet();
        String[] splits = clickMenuId.split(",");
        for (String var : splits) {
            if (StringUtils.isNotBlank(var)) {
                ids.add("'" + var + "'");
            }
        }
        // 只有clickMenuId有效存在 才触发后续操作
        if (ids.size() > 0) {
            String clickMenuIdFixed = Joiner.on(",").join(ids);
            // 删除菜单下面存在的权限 
            rolepurviewDao.deleteByRoleIdAndClickMenu(roleId, clickMenuIdFixed);
            if (StringUtils.isNotBlank(checkPurviewId)) {
                List<Rolepurview> rolepurviews = new ArrayList<Rolepurview>();
                String[] purIds = checkPurviewId.split(",");
                if (purIds != null && purIds.length > 0) {
                    for (String purId : purIds) {
                        Rolepurview rolepurview = new Rolepurview();
                        String id = IdFactory.getUUIDUpper();
                        rolepurview.setId(id);
                        rolepurview.setSpurviewId(purId);
                        rolepurview.setSroleId(roleId);
                        rolepurviews.add(rolepurview);
                    }
                    //批量插入权限记录
                    if (CollectionUtils.isNotEmpty(rolepurviews)) {
                        rolepurviewDao.insertByBatch(rolepurviews);
                    }
                }
            }
        }
    }

    @Override
    public List<Role> selectOperatorNoCheckRole(String userId) {
        return roleDao.selectOperatorNoCheckRole(userId);
    }

    @Override
    public List<Role> selectByUserId(String userId) {
        return roleDao.selectByUserId(userId);
    }

    @Override
    public Page<Role> selectPageByDomainWhere(Page<Role> page, Role role) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<Role>)roleDao.selectByDomainWhere(role);
    }

    @Override
    public List<Role> selectAll() {
        return roleDao.selectByEntityWhere(new Role());
    }
}